<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="800px"
    @close="resetForm"
  >
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="12">
          <el-form-item label="账户" prop="account">
            <el-input v-model="formData.account" placeholder="账户" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="充值金额" prop="money">
            <el-input v-model="formData.money" type="number" placeholder="充值金额" clearable>
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="descri">
        <el-input v-model="formData.descri" type="textarea" rows="4" placeholder="备注" clearable />
      </el-form-item>
      <el-form-item label="图片" prop="picture">
        <img-upload name="recharge" :image-url="imageUrl" @onSuccess="onSuccess" @beforeUpload="beforeUpload" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import ImgUpload from '@/components/ImgUpload'
import UploadMixin from '@/mixin/UploadMixin'
export default {
  components: {
    ImgUpload
  },
  mixins: [UploadMixin],
  data() {
    return {
      title: '新增记录',
      dialogVisible: false,
      statuses: [{ value: 1, text: '正常' }, { value: 0, text: '异常' }],
      formData: {
        account: '',
        money: '',
        picture: '',
        descri: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账户', trigger: 'blur' }
        ],
        money: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    imageUrl() {
      if (this.formData.picture) {
        let prefix = ''
        if (!/http:\/\/|https:\/\//.test(this.formData.picture)) {
          prefix = `${process.env.VUE_APP_BASE_DOMAIN}`
        }
        return prefix + this.formData.picture
      }
      return ''
    }
  },
  methods: {
    onSuccess(data) {
      this.formData.picture = data.imgUrl
    },
    show(row) {
      if (row) {
        this.formData = row
      }
      this.dialogVisible = true
    },
    hide() {
      this.dialogVisible = false
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submitForm', this.formData)
          this.dialogVisible = false
          this.resetForm()
        } else {
          console.log(valid)
        }
      })
    },
    resetForm() {
      this.formData = {
        account: '',
        money: '',
        picture: '',
        descri: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
